import { escapeHtml } from './escapeHtml'
import store from '@/store'
export default {
  richtext: {
    bind: (el, binding) => {
      el.innerHTML = escapeHtml(binding.value)
    },
    componentUpdated: (el, binding) => {
      el.innerHTML = escapeHtml(binding.value)
    }
  },
  hasRole: {
    inserted: (el, binding) => {
      const roles = store.state.userInfo.roles || ['user']
      if (!roles.includes(binding.value)) {
        el.parentNode.removeChild(el)
      }
    }
  },
  hasPermission: {
    inserted: (el, binding, vnode) => {
      const type = vnode.context.$route.meta.type
      const permission = binding.value.split(',')
      const flag = permission.every((item) => type.includes(item))
      if (!flag) {
        el.parentNode.removeChild(el)
      }
    }
  }
}
